package service

import (
	"boy-go/modules/system/model"
	"boy-go/modules/system/repository"
	"boy-go/modules/system/utils/dict"
	"boy-go/modules/system/vo"
	"boy-go/pkg/request"
	"context"
	"github.com/jinzhu/copier"
)

func NewSysDictTypeService() SysDictTypeService {
	return &sysDictTypeService{
		SysDictTypeRepo: repository.NewSysDictTypeRepository(),
		SysDictDataRepo: repository.NewSysDictDataRepository(),
	}
}

type sysDictTypeService struct {
	SysDictTypeRepo *repository.SysDictTypeRepository
	SysDictDataRepo *repository.SysDictDataRepository
}

// GetById 根据id获取配置信息
func (s *sysDictTypeService) GetById(dictId int64) (*vo.SysDictTypeModel, error) {
	m, err := s.SysDictTypeRepo.GetById(dictId)
	if err != nil {
		return nil, err
	}
	rsp := new(vo.SysDictTypeModel)
	if err := copier.Copy(rsp, m); err != nil {
		return nil, err
	}
	return rsp, nil
}

// List 获取字典类型表列表
func (s *sysDictTypeService) List(req *vo.QuerySysDictTypeReq) (*vo.ListSysDictTypeRsp, error) {
	var pageReq = &request.PageRequest{
		PageNum:  req.PageNum,
		PageSize: req.PageSize,
	}
	list, total, err := s.SysDictTypeRepo.List(req, pageReq)
	if err != nil {
		return nil, err
	}
	itemList := make([]*vo.SysDictTypeModel, 0)
	for _, v := range list {
		item := new(vo.SysDictTypeModel)
		if err := copier.Copy(item, &v); err != nil {
			continue
		}
		itemList = append(itemList, item)
	}
	return &vo.ListSysDictTypeRsp{
		List:  itemList,
		Total: total,
	}, nil
}

// Add 添加字典类型表
func (s *sysDictTypeService) Add(req *vo.AddSysDictTypeReq) (*vo.AddSysDictTypeRsp, error) {
	m := new(model.SysDictType)
	err := copier.Copy(m, req)
	if err != nil {
		return nil, err
	}
	if err := s.SysDictTypeRepo.Save(m); err != nil {
		return nil, err
	}
	return &vo.AddSysDictTypeRsp{
		DictId: m.DictId,
	}, nil
}

// Edit 修改字典类型表
func (s *sysDictTypeService) Edit(req *vo.EditSysDictTypeReq) (*vo.EditSysDictTypeRsp, error) {
	m := new(model.SysDictType)
	err := copier.Copy(m, req)
	if err != nil {
		return nil, err
	}
	if err := s.SysDictTypeRepo.Update(m); err != nil {
		return nil, err
	}
	return &vo.EditSysDictTypeRsp{
		DictId: m.DictId,
	}, nil
}

// Del 删除字典类型表
func (s *sysDictTypeService) Del(dictId int64) error {
	return s.SysDictTypeRepo.Delete(dictId)
}

func (s *sysDictTypeService) BatchDel(ids []string) error {
	return s.SysDictTypeRepo.BatchDelete(ids)
}

func (s *sysDictTypeService) SelectDictTypeAll() ([]*vo.SysDictTypeModel, error) {
	list, _, err := s.SysDictTypeRepo.List(nil, nil)
	if err != nil {
		return nil, err
	}
	itemList := make([]*vo.SysDictTypeModel, 0)
	for _, v := range list {
		item := new(vo.SysDictTypeModel)
		if err := copier.Copy(item, &v); err != nil {
			continue
		}
		itemList = append(itemList, item)
	}
	return itemList, nil
}

// SelectDictDataByType 根据字典类型查询字典数据
func (s *sysDictTypeService) SelectDictDataByType(dictType string) ([]*vo.SysDictDataModel, error) {
	ctx := context.Background()
	dictDatas := dict.GetDictData(&ctx, dictType)
	if dictDatas != nil {
		return s.handlerDictDatas(dictDatas), nil
	}
	dictDatas, err := s.SysDictDataRepo.SelectDictDataByType(dictType)
	if err != nil {
		return nil, err
	}
	dict.SetDictData(&ctx, dictType, dictDatas)
	return s.handlerDictDatas(dictDatas), nil
}

func (s *sysDictTypeService) handlerDictDatas(datas []*model.SysDictData) []*vo.SysDictDataModel {
	voDatas := make([]*vo.SysDictDataModel, 0)
	if datas != nil {
		for _, data := range datas {
			var voData = new(vo.SysDictDataModel)
			err := copier.Copy(&voData, data)
			if err != nil {
				continue
			}
			voDatas = append(voDatas, voData)
		}
	}
	return voDatas
}
